<template>
  <div class="app-container">
    <div class="bg_m p_50 flexcenter">
      <el-input
        v-model="input"
        placeholder="请输入模板名称"
        class="search"
        clearable
        @keyup.enter.native="handleQuery"
      >
        <i slot="suffix" class="el-icon-search f-22 searchicon" @click="handleQuery"></i>
      </el-input>
    </div>
    <div class="navbar">
      <div class="innerwrapper2">
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          active-text-color="#4A73FF"
          text-color="#000"
          mode="horizontal"
          @select="handleSelect"
        >
          <el-menu-item index="">全部</el-menu-item>
          <el-menu-item
            v-for="(item, index) in navbar"
            :index="item.id+''"
            :key="index"
            >{{ item.name }}</el-menu-item
          >
        </el-menu>
      </div>
    </div>
    <div class="listbody bg_f4">
      <div class="innerwrapper2 pt_40 boxh"  style="overflow: auto" 
        v-infinite-scroll="load" 
        immediate-check="false"
        :infinite-scroll-distance='50'
        :infinite-scroll-disabled="loading">
        <div class="flex flexwrapper ">
          <div class="listitem mb_40 commhover"
            v-for="(item, index) in list"
            :key="index"
          >
            <div class="bg_temp">
              <!-- <img src="@/assets/images/phonetemp.png" class="phonebg" alt=""> -->
              <div class="itembg flexcenter">
                <el-button type="primary" size="small" @click="handleOpen(item)">创建活动</el-button>
              </div>
              <img :src="item.img"
                alt=""
                class="imgbg"
              />
            </div>
            <div class="titlename f-15">{{item.name}}</div>
          </div>
        </div>
        <div class="flexcenter f-14 c_606 pb_15" v-if="isend">没有更多了</div>
        <div class="flexcenter f-14 c_606 pb_15" v-if="loading">加载中...</div>
      </div >
    </div>

    <el-dialog
      title=""
      :visible.sync="open"
      width="800px"
      top="20vh"
      :before-close="handleClose">
      <div class="flexrowbetween diabody" v-if="open">
        <div class="phoneleft">
          <iframe width="100%" height="100%" :src="iframeUrl" frameborder="0"></iframe>
        </div>
        <div class="phoneright">
          <div class="mb_25 c_000 f-22">{{info.name}}</div>
          <div class="mb_25 c_606 f-16">模板简介：{{info.msg || '暂无'}}</div>
          <div class="flex">
            <div class="itemd f-13 c_606 mr_5">{{info.template_class.name}}</div>
            <div class="itemd f-13 c_606 mr_5">{{info.template_name}}</div>
            <!-- <div v-for="(item,index) in 5" :key="index" class="itemd f-13 c_606 mr_5">{{info.template_name}}</div> -->
          </div>
          <div class="codebox">
            <div class="code" ref="qrCodeUrl"></div>
            <!-- <img class="code" src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQF68DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyR29xclp2bURhaWUxeTJySzFBY0oAAgQCyqxkAwSAUQEA" alt=""> -->
          </div>
          <div class="textcenter f-14 c_606 mt_10">扫码预览</div>
          <div class="subbtn c_fff f-16 flexcenter commhover" @click="createActive">立即创建</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from 'qrcodejs2'
import { templateClass, templateIndex, templateInfo } from '@/api/template'
import { taskAdd } from '@/api/active';
export default {
  data() {
    return {
      navbar: [],
      input:'',
      activeIndex: '',
      loading: false,
      count:20,
      open:false,
      list:[],
      total:0,
      queryParams:{
        page:1,
        num:30
      },
      isend:false,
      info:null,
      iframeUrl:''
    };
  },
  created(){
    this.getClass()
    this.getList()
  },

  methods:{
    
    getClass(){
      templateClass().then(res=>{
        this.navbar = res.data
      })
    },

    handleQuery(){
      this.list = []
      this.loading = false
      this.isend = false
      this.queryParams.page = 1
      this.getList()
    },

    getList(){
      this.loading = true
      console.log('this.activeIndex:',this.activeIndex)
      let params = {...this.queryParams}
      if(this.activeIndex){
        params.class_id = this.activeIndex
      }
      if(this.input){
        params.keyword = this.input
      }
      templateIndex(params).then(res=>{
        this.list = this.list.concat(res.data.list.map(item=>({...item,img: process.env.VUE_APP_BASE_API + '/' + item.url})))
        this.total = res.data.count
        if(this.list.length == this.total){
          this.isend = true
        }
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },

    handleSelect(val){
      console.log('val',val,this.activeIndex)
      if(val!==this.activeIndex){
        this.list = []
        this.loading = false
        this.isend = false
        this.activeIndex = val
        this.queryParams.page = 1
        this.getList()
      }
    },
    
    load() {
      if(this.isend){
        return
      }
      console.log('加载')
      this.loading = true
      this.queryParams.page ++
      this.getList()
    },

    handleOpen(row){
      templateInfo({id:row.id}).then(res=>{
        this.info=res.data
        this.info.template_class = row.template_class
        this.iframeUrl = this.h5domain + '?id='+res.data.task_id+'&istest=1'
        this.open = true
        // console.log('this.iframeUrl：',)
        this.$nextTick(()=>{
          var qrcode = new QRCode(this.$refs.qrCodeUrl, {
            text: this.h5domain + '?id='+res.data.task_id,
            width: 160,
            height: 160,
            colorDark : "#000000",
            colorLight : "#ffffff",
            correctLevel : QRCode.CorrectLevel.Q
          });
        })
        
      })
      
    },

    handleClose(){
      this.open = false
    },

    createActive(){
      let params = {
        template_id:this.info.id
      }
      if(localStorage.getItem('admin_id')&&localStorage.getItem('admin_id')!=='undefined'){
        params.admin_id = localStorage.getItem('admin_id')
      }
      taskAdd(params).then(res=>{
        this.$router.push({name:"CreateTemp",query:{id:res.data}})
      })
    },
  }
};
</script>

<style lang="scss" scoped>
.app-container{
  height: 100vh;
  // overflow: hidden;
}
.search {
  width: 520px;
  ::v-deep .el-input__inner {
    height: 40px;
    line-height: 40px;
    border-radius: 40px;
    border: 1px solid #dcdcdc;
  }
}
.el-menu {
  border: 0;
}
.el-menu-item.is-active {
  border: 0;
}
.listitem {
  width: 25%;
  margin-bottom: 40px;
  // height: 470px;
  padding: 0 12px;
 
  .bg_temp{
    width: 224px;
    height: 424px;
    // height: 475px;
    border-radius: 3px;
    position: relative;
    overflow: hidden;
    background: url('~@/assets/images/phonetemp.png') no-repeat;
    background-size: cover;
    padding: 24px 4px 0 5px;
    border-radius: 24px;
    margin: auto;
    // .phonebg{
    //   position: absolute;
    //   top: 0;
    // }
    &:hover .itembg{
      display: flex!important;
     
    }
  }
  .itembg{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    
    background-color: rgba($color: #000000, $alpha: 0.6);
  }
  .imgbg {
    width: 100%;
    // height: 100%;
    height: calc(100% - 5px);
    border-radius: 4px 4px 22px 20px;
    // height: 340px;
    // line-height: 342px;
    // border-radius: 10px;
  }
}
.titlename {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  color: #000;
  overflow: hidden;
  padding: 0 10px;
  text-overflow: ellipsis;
}
.boxh{
  height: calc(100vh - 268px);
  // height: 668px;
  // height: 400px;

  &::-webkit-scrollbar {
    display: none;
  }
}
.bg_m{
  background: url('~@/assets/images/mbanner.png') no-repeat 50% 50%;
}
.phoneleft{
  width: 280px;
  height: 460px;
  background-color: #eee;
  border-radius: 6px;
  overflow: hidden;
}
.diabody{
  padding: 0 30px 30px;
  align-items: flex-start;
}
.phoneright{
  width: 360px;
  padding-left: 22px;
}
.itemd{
  min-width: 42px;
  text-align: center;
  padding: 3px 10px;
  border: 1px solid #eee;
  border-radius: 16px;
  margin-bottom: 20px;
}
.codebox{
  width: 160px;
  height: 160px;
  margin: auto;
  margin-top: 40px;
  img{
    width: 100%;
    height: 100%;
  }
}
.subbtn{
  width: 76%;
  margin: auto;
  background-color: #4A73FF;
  height: 40px;
  margin-top: 45px;
}
.searchicon{
  display: inline-block;
  margin: auto;
  line-height: 40px;
  padding-right: 10px;
  cursor: pointer;
}
.el-menu-demo{
  .el-menu-item{
    font-size: 14px;
    height: 52px;
    line-height: 52px;
  }
}
</style>